//
//  MCLogisticsViewController.swift
//  StoreManage
//
//  Created by MC on 2017/12/21.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCLogisticsViewController: MCViewController {

    var orderSn = ""
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    
    // MARK: - Network
    func sendNetworking() {
        let para = [
            "orderSn" : "151279451168799933",  // 3967953607539
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_getLogistics, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
            let dataDict = response["data"] as! [String : AnyObject]
            let goodImgPath = dataDict["goodImgPath"] as! String
            let isFinish = dataDict["isFinish"] as! NSNumber
            let logisticsSessage = dataDict["logisticsSessage"] as! String

            if isFinish.intValue == 0 {
                self.headerView.stautsLabel.text = "物流状态：" + "进行中"
            } else {
                self.headerView.stautsLabel.text = "物流状态：" + "已签收"
            }
            self.headerView.iconImageView.sd_setImage(with: URL.init(string: goodImgPath), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
           
            let jsonData = logisticsSessage.data(using: String.Encoding.utf8)
            let jsonObject : [String : Any] = try! JSONSerialization.jsonObject(with: jsonData!, options: .mutableContainers) as! [String : Any]

            let company = jsonObject["company"] as! String
            let no = jsonObject["no"] as!String
            
            self.headerView.nameLabel.text = "物流公司:" + company
            self.headerView.numberLabel.text = "物流单号:" + no
            
            let list = jsonObject["list"] as! NSArray
            
            for dict in list {
                let model = MCLogisticsModel()
                model.setValuesForKeys(dict as! [String : Any])
                self.dataArrayM.add(model)
            }
            
            self.tableView.reloadData()
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "物流信息"
        self.view.backgroundColor = UIColor.white
        tableView.tableHeaderView = headerView
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
    }
    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var headerView: MCLogisticsheaderView = {
        let view = MCLogisticsheaderView()
        return view
    }()
}

extension MCLogisticsViewController : UITableViewDelegate,UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = self.dataArrayM[indexPath.row] as! MCLogisticsModel
        let detail = model.remark
        let height = detail.MCString_getHeight(font: MCFont(14), width: MCScreenWidth - 60)

        return height + 50
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCLogisticsCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.model = self.dataArrayM[indexPath.row] as! MCLogisticsModel
        cell.row = indexPath.row
        return cell
    }
}

